﻿using System;
using System.Collections.Generic;
using System.Text;
using Tools;

namespace MtwControl
{
    /// <summary>
    /// 鼠标动作对象，包含鼠标移动，左键，右键按下等操作
    /// </summary>
    public class MouseObject
    {
        private int mouseKind;
        private int x;
        private int y;
        private static Log log;
        //MouseLeft__,mouseRight
        //MouseWheel,MouseMove
        /// <summary>
        /// 鼠标动作对象的构造函数，
        /// </summary>
        /// <param name="msg">为鼠标消息，消息结构请参考MTW手机端格式</param>
        public MouseObject()
        {
            log = new Log(this);
        }
        public void excute(String msg)
        {
            log.debug(msg);
            String mousekindStr = msg.Split('-')[0].Trim();
            if (mousekindStr.Equals("MouseLeft") || mousekindStr.Equals("MouseRight"))
            {
                mousekindStr = mousekindStr + msg.Split('_')[1].Trim();
            }
            int[] code = getCode(mousekindStr, msg);
            //处理
            ExternFun.mouse_event(code[0], code[1], code[2], code[3], code[4]);
            Console.WriteLine(msg);
        }

        private int[] getCode(String mousekindStr,String msg)
        {
            if (mousekindStr.Equals("MouseRightDOWN"))
            {
                log.debug("MouseRightDOWN");
                mouseKind = MouseKword.MOUSEEVENTF_RIGHTDOWN;
                return new int[] { mouseKind,0, 0,0,0 };
            }
            if (mousekindStr.Equals("MouseRightUP"))
            {
                log.debug("MouseRightUP");
                mouseKind = MouseKword.MOUSEEVENTF_RIGHTUP;
                return new int[] { mouseKind, 0, 0, 0, 0 };
            }
            if (mousekindStr.Equals("MouseLeftDOWN"))
            {
                log.debug("MouseLeftDOWN");
                mouseKind = MouseKword.MOUSEEVENTF_LEFTDOWN;
                return new int[] { mouseKind, 0, 0, 0, 0 };
            }
            if (mousekindStr.Equals("MouseLeftUP"))
            {
                log.debug("MouseLeftUP");
                mouseKind = MouseKword.MOUSEEVENTF_LEFTUP;
                return new int[] { mouseKind, 0, 0, 0, 0 };
            }
            if (mousekindStr.Equals("MouseWheel"))
            {
                log.debug("MouseWheel");
                mouseKind = MouseKword.MOUSEEVENTF_WHEEL;
                double y = Convert.ToDouble(msg.Split(':')[1].Trim());
                if (y > 0)
                {
                    return new int[] { mouseKind, 0, 0, -120, 0 };
                }
                else
                {
                    return new int[] { mouseKind, 0, 0, 120, 0 };
                }
               
            }
            if (mousekindStr.Equals("MouseMove"))
            {
                log.debug("MouseMove");
                mouseKind = MouseKword.MOUSEEVENTF_MOVE;
                x = Convert.ToInt32(msg.Split(':')[1].Replace("-Y", "").Trim().Split('.')[0]);
                y = Convert.ToInt32(msg.Split(':')[2].Trim().Split('.')[0]);
                return new int[] { mouseKind, x, y, 0, 0 };
            }
            return null;
        }
    }
}
